<?php

class PromotionsController extends Controller
{
  public $layout='//layouts/admin';

  public function filters()
  {
    return array(
      'accessControl',
    );
  }

  public function accessRules()
  {
    return array(
      array('allow',
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow',
        'actions'=>array('create','update','list','delete','updateStatus','upDisplay','downDisplay'),
        'users'=>array('@'),
      ),
      array('deny',  // deny all users
        'users'=>array('*'),
      ),
    );
  }

  public function actionIndex()
  {
    $this->layout = '//layouts/main';
/*
    $pageModel = Page::model()->find(array(
      'condition'=>'slug=:slug',
      'params'=>array(':slug' => 'promotion'),
    ));
*/
    $dataProvider=new CActiveDataProvider('Promotions',
      array (
       'criteria' => array(
        'condition' => 'enable=true AND photo_path is not null',
        'order'=>'display_order ASC'
       )
      )
    );

    $this->metaKeyword = 'Promotion : '.$this->metaKeyword;
    //$this->metaDescription = strip_tags($pageModel->description);

    $this->render('index',compact('dataProvider'));
  }

  public function actionView($data)
  {
    $this->layout = '//layouts/main';
    $model=Promotions::model()->find(array(
      'condition'=>'enable=:enable AND slug=:slug',
      'params'=>array(':enable' => true, ':slug' => $data),
    ));
    $modelsList = Promotions::model()->findAll(array(
      'condition'=>'enable=:enable AND id !=:id',
      'params'=>array(':enable' => true, ':id' => $model->id),
    ));

    $this->metaKeyword = $model->name_en. ' : '.$this->metaKeyword;
    $this->metaDescription = strip_tags($model->description);

    $this->render('view', compact('model','modelsList'));
  }

  public function actionCreate()
  {
    $model=new Promotions;

    if(isset($_POST['Promotions']))
    {
      $model->attributes=$_POST['Promotions'];
      $model->savePhoto();
      if($model->save()) {
        $this->redirect(array('list'));
      }
    }

    $this->render('create',array(
      'model'=>$model,
    ));
  }

  public function actionUpdate($id)
  {
    $model=$this->loadModel($id);

    if(isset($_POST['Promotion']))
    {
      $model->attributes=$_POST['Promotion'];
      $model->savePhoto();
      if($model->save())
        $this->redirect(array('list'));
    }

    $this->render('update',array(
      'model'=>$model,
    ));
  }

  public function actionUpDisplay($id)
  {
      $model = $this->loadModel($id);
      $display_order = $model->display_order-1;
      $prev_model = Promotions::model()->find(array(
        'condition'=>'display_order=:display_order',
        'params'=>array(':display_order' => $display_order),
      ));
      $prev_model->display_order = $prev_model->display_order+1;

      $model->display_order = $display_order;
      $prev_model->save();
      $model->save();
      $this->redirect(array('list'));
  }

  public function actionDownDisplay($id)
  {
      $model = $this->loadModel($id);
      $display_order = $model->display_order+1;

      $max_display_order = $model->getMaxDisplayOrder();

      $next_model = Promotions::model()->find(array(
        'condition'=>'display_order=:display_order',
        'params'=>array(':display_order' => $display_order),
      ));
      if ($model->display_order < $max_display_order) {
        $next_model->display_order = $next_model->display_order-1;

        $model->display_order = $display_order;
        $next_model->save();
        $model->save();
      }
      $this->redirect(array('list'));

  }

  public function actionDelete($id)
  {
    if(Yii::app()->request->isPostRequest)
    {
      $model = $this->loadModel($id);

      Promotions::model()->updateCounters(
        array('display_order'=> -1),
        'display_order >:display_order',
        array(':display_order' => $model->display_order)
      );

      $model->active_flag = 0;
      $model->enable= 0;
      $model->display_order = 0;
      $model->save();

      if(!isset($_GET['ajax']))
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    else
      throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  }

  public function actionList()
  {
    $dataProvider=new CActiveDataProvider('Promotions',
      array (
       'criteria' => array(
        'condition' => 'active_flag=true',
        'order'=>'display_order ASC'
       )
      )
    );
    $this->render('list',array(
      'dataProvider'=>$dataProvider,
    ));
  }

  public function actionUpdateStatus($id)
  {
    $model = $this->loadModel($id);

    $model->enable = ($model->enable==true) ? 0 : 1;
    $model->save();
    $this->redirect(array('list'));

  }

  public function loadModel($id)
  {
    $model=Promotions::model()->findByPk($id);
    if($model===null)
      throw new CHttpException(404,'The requested page does not exist.');
    return $model;
  }

  protected function performAjaxValidation($model)
  {
    if(isset($_POST['ajax']) && $_POST['ajax']==='Promotion-type-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }
  }
}
